package drawing;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;
import malaria_acd.AddNewNameActivity;
import project.malaria.malaria_acd.R;
import util.DrawView;
import util.MessageUtil;
import util.SessionUtil;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class DrawHousenNoActivity extends Activity {
	/***Variable:: Drawing and file***/
	DrawView drawView,drawView2;
	DrawView mView;
	private RelativeLayout houseno;
	private Bitmap b_houseno;

	/***Variable :: Widget***/
	private Button saveimgs;
	private ImageView back;

	/***Variable:: SharedPreference***/
	public static final String PREFS_NAME = "Drawing";
	public static  String shDrawHouse;
	private  SharedPreferences share; 
	
	/***Variable:: Session***/
	private String province,district,subdistrict,moo,village;
	private String ACD_ID;
	private String root;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drawhouseno);
		initializeValue();
		getSession();
		drawView = new DrawView(this);
		houseno.addView(drawView);
		houseno.setDrawingCacheEnabled(true);
		houseno.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		houseno.layout(0, 0, houseno.getMeasuredWidth(), houseno.getMeasuredHeight()); 
		houseno.buildDrawingCache(true);
		houseno.setDrawingCacheEnabled(false);
		 
		saveimgs.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	        View v1 = getWindow().getDecorView().findViewById(R.id.signImageFname);
	      		v1.setDrawingCacheEnabled(true);
	      		b_houseno = Bitmap.createBitmap(v1.getDrawingCache());
	      		v1.setDrawingCacheEnabled(false);
	      	  	String fname = SaveImage(b_houseno,"hn");
	      		      		     	
	        	sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 

	        	Intent i = new Intent(DrawHousenNoActivity.this, AddNewNameActivity.class);
	        				SharedPreferences.Editor edit = share.edit();
	        	   	 		edit.putString(shDrawHouse,fname);
	        	   	 		edit.commit();
	        	   	 				SessionUtil.setValue("pathHouse",fname);
	        	   	 				SessionUtil.setValue("typeHouse","1"); 
	        	   	 				SessionUtil.setValue("trigger","1");	
	        	   	 			finish();
	        startActivityForResult(i,0);	
	        
	        }
	    });		
	
back.setOnClickListener(new View.OnClickListener() {
	public void onClick(View v) {
		// TODO Auto-generated method stub
		 onBackPressed();
	}
});		
	
	}
	private String SaveImage(Bitmap finalBitmap,String filetype) {
	    String root = Environment.getExternalStorageDirectory().toString();
	    File myDir = new File(root + "/ACD/images");    
	    myDir.mkdirs();
	       
	   String  fname = filetype + ACD_ID+ ".jpg";	    
	     
	    File file = new File (myDir, fname);
	    if (file.exists ()) file.delete (); 
	    try {
	           FileOutputStream out = new FileOutputStream(file);
	           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
	           out.flush();
	           out.close();         

	    } catch (Exception e) {
	           e.printStackTrace();	           
	    }
	    return fname;
	    
	}
	
	public void funcClear(View v) {		
			funcClear();
		}	
	public void funcClear() {
			drawView.requestFocus();   
			drawView.clear();
		}	
	
	
	private void initializeValue()
	{
		share = this.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
		root = Environment.getExternalStorageDirectory().toString();
		houseno = (RelativeLayout) findViewById(R.id.signImageFname);
		back = (ImageView) findViewById(R.id.imageView1);
		saveimgs = (Button) findViewById(R.id.saveimg);
	}	
	public void getSession()
	{	
		 ACD_ID 		= SessionUtil.getValue("NEWACD_ID");
	}
}
